define(function(require, exports){
	var $		= require("jquery");
	var pubjs	= require('pubjs');
	var popwin	= require('@base/dialog').base;

	var SwitchPop = popwin.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'title': LANG('切换用户'),
				'showFoot':0,
				'width': 500,
				'class': 'P-login',
				'id': null,
				'view_model': {
				}
			});

			this.Super('init', arguments);
		},
		afterBuild:function(){
			this.Super('afterBuild',arguments);
			var el = this.getDOM('body');
			var cls = this.getConfig('class');
			el.addClass(cls);
			el.append($([
				'<div class="switchUserPop">',
					'<p>'+ LANG('当前登录的账号登录不成功,请选择下面的账号进行切换.') +'</p>',
					'<span pub-mod="@base/common/drop.drop" pub-name="list"></span>',
					'<div pub-name="save" pub-mod="@base/common/input.button" ></div>',
				'</div>'
			].join('')));

			this.$animate = el.find('.animated');

			var config = {
				'list': {
					'Name': 'UserName',
					'Key': 'UserId',
					'def': LANG('选择切换的用户'),
					'name': 'UserName',
					'key': 'UserId',
					'width': 280,
					'panel_config': {
						'height': 200
					}
				},
				'save': {
					'value': LANG('切换'),
					'class': 'btn btn-success mt10'
				}
			};

			this.buildFromTemplate(config);
		},
		afterBuildTemplate: function(){
		},
		setData: function(data){
			if(data){
				this.$data = data.switchList;
				if(this.$.list){
					this.$.list.setData(this.$data);
				}
				this.$userInfo = data.userInfo;
				this.$code = data.code;
				if(data.switchList.length == 1) {
					sendLogin(this, this.$data[0].UserId);
				}
			}
		},
		onInputClick: function(ev){
			var self = this;
			if (ev.from === self.$.save){
				var data = self.$.list.getValue();
				if(!data || data.UserId == -1){
					pubjs.alert(LANG('请先选择要切换的账号'));
					return false;
				}

				sendLogin(self, data.UserId);
			}
			return false;
		},
		onSaveSuccess: function(evt){
			//新建创意包页面
			this.onCancel();
			return false;
		}
	});

	function sendLogin(self, id) {
		pubjs.mc.send('/switchLogin',{id: id, userInfo: self.$userInfo, code: self.$code}, function(err, da){
			if(err){
				pubjs.alert(err.message);
				return false;
			}
			self.fire('switchSuccess', da);
			self.hide();
		});
	}

	exports.base = SwitchPop;

});